import pandas as pd
import json
import jieba
import re
from gensim.models import KeyedVectors
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import font_manager

# 设置中文字体
font_path = 'C:/Windows/Fonts/simhei.ttf'  # 请替换为系统中实际存在的中文字体路径
font_prop = font_manager.FontProperties(fname=font_path)
plt.rcParams['font.family'] = font_prop.get_name()

# 加载预训练的中文Word2Vec模型
model_path = r"D:\实习\word2vec_model2.txt"#替换为实际路径
word_vectors = KeyedVectors.load_word2vec_format(model_path, binary=False)

# 读取Excel文件
text_excel_file = r"D:\实习\cleaned_data.xlsx"#替换为实际路径
comparison_excel_file = r"D:\实习\数据集.xlsx"  # 此数据集为游戏名词清单

# 读取文本数据和比较词汇数据
text_df = pd.read_excel(text_excel_file)
comparison_df = pd.read_excel(comparison_excel_file)

# 获取第一列数据
texts_to_compare = text_df.iloc[:, 0].tolist()

# 获取比较词汇列表
comparison_words = comparison_df.iloc[:, 0].tolist()

def filter_words(words):
    filtered_words = []
    for word in words:
        # 使用正则表达式过滤非中文字符
        if re.match(r'^[\u4e00-\u9fa5]+$', word) and word not in ["恭喜", "获得"]:
            filtered_words.append(word)
    return filtered_words

def calculate_highest_similarity(text, comparison_words):
    max_similarity = 0
    most_similar_word_from_text = None
    most_similar_word_from_comparison = None
    words = jieba.lcut(text)  # 使用jieba进行分词
    words = filter_words(words)  # 过滤非中文字符和特定词汇
    found = 0
    for word1 in words:
        for word2 in comparison_words:
            try:
                similarity = word_vectors.similarity(word1, word2)
                if similarity > max_similarity:
                    max_similarity = similarity
                    most_similar_word_from_text = word1
                    most_similar_word_from_comparison = word2
                if max_similarity == 1:
                    found = 1
                    break
            except KeyError:
                # 处理词汇不在词汇表中的情况
                continue
        if found == 1:
            break
    return max_similarity, most_similar_word_from_text, most_similar_word_from_comparison

# 设置阈值列表
threshold_values = [0.90]

# 存储结果的列表
results = []

# 遍历每个阈值
for threshold_value in threshold_values:
    max_similarities = []
    most_similar_words_from_text = []
    most_similar_words_from_comparison = []
    categories = []

    # 逐个处理每个文本并保存最高相似度值和对应的词
    for text in texts_to_compare:
        similarity_score, word_from_text, word_from_comparison = calculate_highest_similarity(text, comparison_words)
        max_similarities.append(similarity_score)
        most_similar_words_from_text.append(word_from_text if word_from_text else "无")
        most_similar_words_from_comparison.append(word_from_comparison if word_from_comparison else "无")
        # 根据相似度设置类别
        category = 1 if similarity_score > threshold_value else 0
        categories.append(category)

    # 将结果保存到DataFrame
    text_df['最高相似度'] = max_similarities
    text_df['最高相似度文本词'] = most_similar_words_from_text
    text_df['最高相似度比较词'] = most_similar_words_from_comparison
    text_df['类别'] = categories

    # 计算性能指标
    true_labels = text_df['true_label']  # 请替换为真实标签列的名称
    precision = precision_score(true_labels, categories)
    recall = recall_score(true_labels, categories)
    f1 = f1_score(true_labels, categories)
    accuracy = accuracy_score(true_labels, categories)
    results.append((threshold_value, precision, recall, f1, accuracy))

    # 绘制并保存混淆矩阵
    cm = confusion_matrix(true_labels, categories)
    plt.figure(figsize=(8, 6))
    sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=["预测负类", "预测正类"], yticklabels=["实际负类", "实际正类"])
    plt.xlabel('预测标签')
    plt.ylabel('实际标签')
    plt.title(f'混淆矩阵 (阈值={threshold_value})')
    plt.savefig(rf"D:\实习\混淆矩阵_{threshold_value}.png")
    plt.close()
    print(f"混淆矩阵图已保存为 混淆矩阵_{threshold_value}.png")

    # 将结果保存到新的Excel文件
    output_excel_file = rf"D:\实习\15{threshold_value}.xlsx"
    text_df.to_excel(output_excel_file, index=False)
    print(f"数据已保存到 {output_excel_file}")

# 打印结果
print("阈值\t\tPrecision\tRecall\t\tF1-score\t\tAccuracy")
for result in results:
    print(f"{result[0]}\t\t{result[1]}\t\t{result[2]}\t\t{result[3]}\t\t{result[4]}")

# 绘制性能指标图表
threshold_values = [result[0] for result in results]
precisions = [result[1] for result in results]
recalls = [result[2] for result in results]
f1_scores = [result[3] for result in results]
accuracies = [result[4] for result in results]

plt.figure(figsize=(10, 6))
plt.plot(threshold_values, precisions, marker='o', label='Precision')
plt.plot(threshold_values, recalls, marker='o', label='Recall')
plt.plot(threshold_values, f1_scores, marker='o', label='F1-score')
plt.plot(threshold_values, accuracies, marker='o', label='Accuracy')
plt.xlabel('Threshold')
plt.ylabel('Score')
plt.title('Performance Metrics at Different Thresholds')
plt.legend()
plt.grid(True)
plt.show()
